#ifndef __TOUCHINPUT__
#define __TOUCHINPUT__

#include "SysUtil.h"

#define MAX_REGIONS 11
#define DRAG_IN_TAP 0

typedef enum {
	STATE_UNKNOWN,
	STATE_DOWN,
	STATE_DRAGGED,
	STATE_UP
} mouseState;

typedef enum {
	SHAPE_UNKNOWN,
	SHAPE_RECT,
	SHAPE_CIRCLE,
} regionShape;

class Region
{
public:
	Region();
	Region(const char* n, regionShape s = SHAPE_UNKNOWN, float sx = -1, float sy = -1, float sw = -1, float sh = -1, float sr = -1);

	char		name[50];
	regionShape shape;
	float			x;
	float			y;
	float			w;
	float			h;
	float			r;

	Region& operator =(Region& s);
};

class Point2D;
class CTouchInput
{
public:
	CTouchInput();

	int		GetMousePosX();
	int		GetMousePosY();
	Point2D GetMousePosition();
	int		GetMouseOldPosX();
	int		GetMouseOldPosY();
	int		GetMouseMoveSpeedX();
	int		GetMouseMoveSpeedY();
	int		GetStartDragX();
	int		GetStartDragY();
	int		GetEndDragX();
	int		GetEndDragY();
	bool	IsMousePressed();
	bool	IsMouseDragging();
	bool	IsMouseDown();
	bool	IsMouseUp();
	bool	IsMouseDownSkipped();
	bool	IsRegionsEnabled();
	void	Clear();

	void	EnableDisableRegions(bool flag);
	bool	AddRegion(Region r);
	bool	RemoveRegion(const char* name);
	bool	ExistsRegion(const char *region);
	void	ClearRegion();
	bool	IsMouseInRegion(const char* name);
	bool	IsRegionPressed(const char* name);
	bool	IsRegionPressed(int id_);
	bool	IsRegionPressedMult(const char* name); // works with overlapping regions
	bool	IsRegionDown(int id_);
	bool	IsRegionDown(const char* name);
	bool	IsRegionDownMult(const char* name); // works with overlapping regions
	int		IndexOfRegion(const char* name);
	int		GetSelectedRegion();
	int		GetPressedRegion();
	void	Update();

public:
	void	SetMousePressed();
	void	SetMouseDragging();
	void	SetMouseFree();
	void	SetMousePos(int x, int y);
	void	SetMouseTime();

	void	InitMouseState();

	bool	isMouseUp;
	bool	isMouseDown;
	bool	isMousePressed;
	bool	isMouseDragging;
	bool	isRegionPressed;

	bool	mouseDownSkiped;

	int		posX;
	int		posY;
	int		oldPosX;
	int		oldPosY;
	int		time;
	int		lastTime;
	int		moveSpeedX;
	int		moveSpeedY;

	int		downRegion;
	int		oldDownRegion;
	int		startDragX;
	int		startDragY;
	int		endDragX;
	int		endDragY;

	bool	isRegionsEnabled;

	Region	activeRegions[MAX_REGIONS];
	int		regionNum;

	int		dragInTap;

	inline bool HasRegionDown() {return downRegion != -1;}	

};

#endif
